jQ( document ).ready(function() {
    //DO NOT MODIFY BELOW THIS LINE

    //**********************************
    jQ("font[style='{visibility: hidden}']" ).parent().remove();

    var element = jQ('#ProgressBar').detach();
    
    if ($('TOCPage') || $('Toc') && (!$('TocSidebarContent'))){
       $('SkinContent').style.paddingTop = "10px";

       if ($('Toc')){
           jQ('#Toc').detach();
       }

       jQ( "div.BorderColor.InnerInner" ).css( "border", "none" );

       courseTitle = "";
       setTitle("");
       updateNavigation();

       myTrace("TOCpage");
    } else {
       jQ('#footerLogo').remove();
       jQ('#SkinContent').before(element);
       $('Footer').style.zIndex = "-1";

       if (jQ('div #Toc strong br')){
           jQ('div #Toc strong br').remove();
       }
    }

    jQ('#Plug').remove();

    closeTOC(true);

    elems = jQ(".QuestionText");

    var showpage = true;

    if (elems){
       for (i=0; i<elems.length;i++){
           if (elems[i].innerHTML.indexOf("ready to complete") > -1){
               myTrace("TOC completion page - all done");
               parent.postMessage('complete', '*');
               Qualtrics.SurveyEngine.navClick(undefined, 'NextButton');
               i=10000;
               showpage = false;
           }
       }
    }

    var vLoading = $("Overlay");
    vLoading.innerHTML = "";

    if (showpage){
       var vPage = $("Wrapper");
       vPage.style.visibility = "visible";
    }

    var elemError = document.getElementsByClassName('HeaderValidationError');

    if (elemError.length>0){
      var errorMsg = document.getElementsByClassName('ValidationError');

      if (errorMsg.length>0){
       errorMsg[0].innerHTML = errorMessageText;
      }
    
      var elemE=elemError[0];

      elemE.children[0].innerHTML = errorText;
      elemE.children[1].children[0].children[1].style.visibility = "hidden";
      elemE.children[1].children[0].children[0].innerHTML = clickToHideText;

      elemE.children[1].children[0].children[0].onclick = function(){
                QualtricsSETools.scrollToDiv('validationError_1');
                elemE.style.visibility = "hidden";
      }
    }
});

/*===================================*/

function startScenario(event){
    myTrace("startScenario");
    Qualtrics.SurveyEngine.navClick(undefined, 'NextButton');
}
    

/*===================================*/

jQ(window).resize(function(){
       updateNavigation();
});

/*===================================*/

function updateNavigation(){
  var videoframe = $('videoiFrame');

  if (!videoframe){
     var questionbox = document.getElementsByClassName('QuestionText BorderColor');
  } else {
    var questionbox = document.getElementsByClassName('ChoiceStructure');
    var vfw1 = videoframe.offsetWidth;
    videoframe.style.height = Math.round((9.0*vfw1)/16.0)+ "px";
  }

  var slideframe = $('slideframe');

  if (slideframe){
    var vfw2 = slideframe.offsetWidth;
    slideframe.style.height = Math.round((9.0*vfw2)/16.0)+ "px";
  }

  var skinWidth = $('SkinContent').offsetWidth;
  var graphicWidth = document.getElementsByClassName("dynamicWidth"); 

  if (graphicWidth){
     for (var j=0;j<graphicWidth.length;++j){ 
       graphicWidth[j].style.width = skinWidth +'px';
     }
  }

  var prev = document.getElementById("newPreviousButton");
  
  if (prev){
    if (skinWidth < 512) {
      prev.value = shortNameForPrev;
      prev.style.fontSize = "8pt";
    } else {
      prev.value = nameForPrev;
      prev.style.fontSize = "12pt";
    }
  }

  var elem = document.getElementsByClassName("END_OF_SURVEY");

  if((!$('TocSidebarContent') && ($('TOCPage') || $('Toc'))) || (elem.length > 0)){
    displayNextButton(false);
    displayPreviousButton(false);
  }

  resizeTitle();
}

function displayNextButton(aState){
  if (aState){
    var nxt = document.getElementById("newNextButton");
    nxt.disabled = false;
    nxt.style.visibility = "visible";
  } else {
    jQ('#newNextButton').remove();
  }
}

function displayPreviousButton(aState){
  if (aState){
    var prev = document.getElementById("newPreviousButton");
    prev.disabled = false;
    prev.style.visibility = "visible";
  } else {
    jQ('#newPreviousButton').remove();
  }
}

/*===================================*/

function closeTOC(){
    var vTOC = document.getElementById("TocSidebarContainer");

    if (vTOC){
       vTOC.className="reduced closed";
    }
}

/*===================================*/

function setTitle(titleString){
    var ct = document.getElementById('coursetitle');
    var pt = document.getElementById('pagetitle');

    if (!pt) {
       pt = '';
       if (titleString) {
          pt = ": <br>" + titleString;
       }
    } else {
       pt = ": <br>" + pt.innerHTML;
    }

    ct.innerHTML = courseTitle + pt;
}

/*===================================*/

function resizeTitle(){
    var head = document.getElementById("Header");
    var skinWidth = $('SkinContent').offsetWidth;
    
    if (jQ.ismobile || (skinWidth < 512)) {
       head.setAttribute("font-size",".8em");
    } else {
       head.setAttribute("font-size", "1em");
    }

    closeTOC();
}

/*===================================*/

function initPage(qDiv){
  setTitle();
  
  $(qDiv).style.visibility = "hidden";
  $(qDiv).style.height = "0px";
  
  updateNavigation();
}

/*===================================*/

function iruDisplayVideo(videoSrc){
  var videoframe = $('videoiFrame');

  var vp = "https://player.vimeo.com/video/" + videoPath[videoSrc] + "?autoplay=1";
  
  videoframe.src = vp;
}
